package com.weng.coder.controller;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.weng.coder.common.utils.Constants;
import com.weng.coder.entity.NameRule;
import com.weng.coder.entity.Project;
import com.weng.framework.core.model.ResponseData;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.weng.coder.service.NameRuleService;
import com.weng.coder.service.ProjectService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 命名规则 Web API
 *
 * @作者 wengzhonghui
 * @创建时间 2018-02-25 16:03:09
 */
@RestController
@RequestMapping("/nameRule") 
@Api(value = "命名规则相关接口")
public class NameRuleController{
	@Resource
	private NameRuleService nameRuleService;
	@Resource
	private ProjectService projectService;
	
	 /**
	 * 获取分页数据
	 */
	 @ResponseBody
	 @RequestMapping(value="/list",method=RequestMethod.GET)
	 @ApiOperation(value = "分页获取命名规则列表", notes = "分页获取命名规则列表")
	 @ApiImplicitParams({
        @ApiImplicitParam(paramType = "query",name = "pageNo",dataType ="Integer" ,required = true,value = "页数"),
        @ApiImplicitParam(paramType = "query",name = "pageSize",dataType ="Integer" ,required = true,value = "每页数量")
	 })
	 public ResponseData getDataByPage(Integer pageNo, Integer pageSize, String projectId)throws Exception{
		 NameRule nameRule = new NameRule();
		 nameRule.setProjectId(projectId);
		 IPage<NameRule> page = nameRuleService.getDataByPage(pageNo, pageSize, nameRule);
		 return ResponseData.success(page);
	 }
	
	 /**
	 * 获取命名规则
	 * 
	 */
	@RequestMapping(value="get-one",method=RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "获取命名规则", notes = "获取命名规则")
	public ResponseData getNameRule(String id, Model model
			,HttpServletRequest request) throws Exception{
		if(StringUtils.isEmpty(id)){
			return ResponseData.failed("命名规则Id不能为空！");
		}
		
		NameRule nameRule = this.nameRuleService.getOne(id);
		return ResponseData.success(nameRule);
	}
	 
	/**
	 * 添加命名规则
	 * 
	 */
	@RequestMapping(value="add",method=RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "添加命名规则", notes = "添加命名规则")
	public ResponseData addNameRule(NameRule nameRule, Model model
			,HttpServletRequest request) throws Exception{
		if(nameRule==null ){
			return ResponseData.failed("命名规则不能为空！");
		}
		Project project = this.projectService.getOne(nameRule.getProjectId());
		if(project==null){
			return ResponseData.failed("添加失败，项目不存在或已经被删除！");
		}else if(project.getIsBuildIn()!=null 
				&& Constants.IS_BUILD_IN_TRUE.compareTo(project.getIsBuildIn())==0){
			return ResponseData.failed("添加失败，该项目是示例模板项目，不允许修改，请在个人项目中修改！");
		}
		nameRuleService.save(nameRule);
		return ResponseData.success("添加命名规则成功！");
	}
	
	/**
	 * 更新命名规则
	 */
	@RequestMapping(value="update",method=RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "更新命名规则", notes = "更新命名规则")
	public ResponseData updateNameRule(NameRule nameRule,HttpServletRequest request) throws Exception{
		if(nameRule==null ){
			return ResponseData.failed("命名规则不能为空！");
		}
		Project project = this.projectService.getOne(nameRule.getProjectId());
		if(project==null){
			return ResponseData.failed("更新失败，项目不存在或已经被删除！");
		}else if(project.getIsBuildIn()!=null 
				&& Constants.IS_BUILD_IN_TRUE.compareTo(project.getIsBuildIn())==0){
			return ResponseData.failed("更新失败，该项目是示例模板项目，不允许修改，请在个人项目中修改！");
		}
		nameRuleService.save(nameRule);
		return ResponseData.success("更新成功！");
	}
	
	
	/**
	 * 删除命名规则
	 */
	@RequestMapping(value="delete",method=RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "删除命名规则", notes = "删除命名规则,多个ID以,号分隔")
	@Transactional
	public  ResponseData deletenameRule( String ids
			,HttpServletRequest request) throws Exception{
		String[] nameRuleIdList = ids.split(",");
		if(nameRuleIdList == null || nameRuleIdList.length < 1){
			return ResponseData.failed("命名规则ID不能为空");
		}
		StringBuilder logDesc = new StringBuilder();
		List<NameRule> nameRuleList = nameRuleService.findByIds(Arrays.asList(nameRuleIdList));
		if(nameRuleList!=null && nameRuleList.size()>0){
			for(NameRule nameRule:nameRuleList){
				Project project = this.projectService.getOne(nameRule.getProjectId());
				if(project==null){
					return ResponseData.failed("更新失败，项目不存在或已经被删除！");
				}else if(project.getIsBuildIn()!=null 
						&& Constants.IS_BUILD_IN_TRUE.compareTo(project.getIsBuildIn())==0){
					return ResponseData.failed("删除失败，该项目是示例模板项目，不允许修改，请在个人项目中修改！");
				}
				if(logDesc.length()>0){
					logDesc.append(",");
				}
				logDesc.append(nameRule.getId());
				this.nameRuleService.delete(nameRule);
			}
		}
		return ResponseData.success("删除成功");
	}


}

